﻿using Quartz;
using SqlSugar;
using Zhao.Common.EnumEntity;
using Zhao.IServer.Field;
using Zhao.IServer.Log;
using Zhao.Models.Entity;
using Zhao.Repository.UnitOfWork;
using Zhao.Service.Field;

namespace Zhao.Arena.Utility.QuartZJob
{
    /// <summary>
    /// 场地预定状态定时修改
    /// </summary>
    public class FielReservationStatusJob : IJob
    {
        private readonly IFieldsServer _fieldsServer;
        protected  ILogsServer _log {  get; set; }
        public FielReservationStatusJob( ILogsServer log, IFieldsServer fieldsServer)
        {
            _fieldsServer=fieldsServer;
            _log = log;
        }

        /// <summary>
        /// 在这里就可以定义job具体做的是什么事儿
        /// 持久化！！！  如果没有持久化 调度任务会一直存在内存中 占用内存
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task Execute(IJobExecutionContext context)
        {
            try
            {
                // 调用服务层方法更新活动状态
                int updateCount = await _fieldsServer.UpdateCompletedAppointmentsAsync();
                await _log.Info($"场地状态定时任务已执行：更新了{updateCount}条记录");
            }
            catch (Exception ex)
            {
                await _log.Error($"场地状态定时任务执行失败：{ex.Message}");
                throw; // 重新抛出异常以便Quartz可以处理作业失败
            }
        }
    }
}
